// Copyright (C) 2013 Michael Leviston (michael.leviston@gmail.com)

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef ECS_MOVESYSTEM_H
#define ECS_MOVESYSTEM_H

#include "SystemBase.h"
#include "Components.h"

using namespace SimpleMath;

namespace ECS
{
	/// Specific node type for this system (allows direct pointers to component members to optimise update loop)
	struct MoveNode
	{
		MoveNode(int NID, Vector3* Position, Vector3* Velocity)
			: nID(NID), position(Position), velocity(Velocity)
		{}

		unsigned int nID;			/// Same node id as generic node in master list
		Vector3* position;
		Vector3* velocity;
	};

	class MoveSystem : public SystemBase<MoveSystem>
	{
	public:

		MoveSystem(int Priority) : SystemBase(Priority) {}

		void Update(float dt)
		{
			std::for_each( moveNodes.begin(), moveNodes.end(), [dt] (MoveNode mn)	
			{
				*mn.position += *mn.velocity * dt;
			} );
		}

		bool MakeNode(spEntity spE)
		{
			if(spE->Has<PositionComponent, VelocityComponent>() )
			{
				/// Add to generic master list
				unsigned int nID = AddGenericNode(spE->Get<PositionComponent>(), spE->Get<VelocityComponent>() );

				/// Add to system specific sub-list
				auto pos = &ECS::MakeStrongPtr(spE->Get<PositionComponent>() )->position;
				auto vel = &ECS::MakeStrongPtr(spE->Get<VelocityComponent>() )->velocity;
				AddSpecificNode(nID, pos, vel);

				return true;
			}
			return false;
		}

		/// Create a move node and add to sub-list
		void AddSpecificNode(unsigned int NID, Vector3* Position, Vector3* Velocity)
		{
			moveNodes.push_back(MoveNode(NID, Position, Velocity) );
		}

		/// Delete any specific nodes in sub-lists
		void DeleteSpecificNode(unsigned int NID)
		{
			for(auto itB = moveNodes.begin(); itB != moveNodes.end(); )
			{
				if(itB->nID == NID)
				{
					itB = moveNodes.erase(itB);
				}
				else
					++itB;
			}
		}

	private:

		std::vector<MoveNode> moveNodes;

	};
}

#endif // ECS_MOVESYSTEM_H